package com.example.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.dto.QueryBookParamsDto;
import com.example.bookstore.entity.Book;
import com.example.bookstore.mapper.BookMapper;
import com.example.bookstore.service.BookService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    BookMapper bookMapper;

    @Override
    //分页条件
    public PageResult<Book> selectByPageAndCondition(int currentPage, int pageSize, QueryBookParamsDto params) {
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(params.getStatus()), Book::getStatus, params.getStatus());
        lqw.like(StringUtils.isNotEmpty(params.getBookName()), Book::getBookName, params.getBookName());
        lqw.like(StringUtils.isNotEmpty(params.getBookId()), Book::getBookId, params.getBookId());
        Page<Book> bookPage = new Page<>(currentPage, pageSize);
        Page<Book> pageResult = bookMapper.selectPage(bookPage, lqw);
        List<Book> bookList = pageResult.getRecords();
        long total = pageResult.getTotal();
        PageResult<Book> bookPageResult = new PageResult<>(bookList, total, currentPage, pageSize);
        return bookPageResult;
    }
    //消费者查询图书条件（已上架）
    @Override
    public Result<String> addBook(Book book) {
        book.setBookImg("http://localhost:8080/image/"+book.getBookImg());
        int insert = bookMapper.insert(book);
        if (insert > 0) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result<String> updateBook(Book book) {
        int update = bookMapper.updateById(book);
        if (update > 0) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    @Override
    public Result<String> updateBookImg(Book book) {
        book.setBookImg("http://localhost:8080/image/"+book.getBookImg());
        int update = bookMapper.updateById(book);
        if (update > 0) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Book selectByBookId(String bookId) {
        return  bookMapper.selectByBookId(bookId);
    }

    @Override
    public Result<String> deleteBook(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            long id = ids.get(i);
            int del = bookMapper.deleteById(id);
            if (del < 1) {
                return Result.error("删除失败");
            }
        }
        return Result.success("删除成功");
    }

    @Override
    public Result<String> up(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            long id = ids.get(i);
            LambdaUpdateWrapper<Book> luw=new LambdaUpdateWrapper<>();
            luw.set(Book::getStatus,1).eq(Book::getId,id);
            int update = bookMapper.update(null, luw);
            if (update < 1) {
                return Result.error("上架失败");
            }
        }
        return Result.success("上架成功");
    }

    @Override
    public Result<String> down(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            long id = ids.get(i);
            LambdaUpdateWrapper<Book> luw=new LambdaUpdateWrapper<>();
            luw.set(Book::getStatus,-1).eq(Book::getId,id);
            int update = bookMapper.update(null, luw);
            if (update < 1) {
                return Result.error("下架失败");
            }
        }
        return Result.success("下架成功");
    }



}

